<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加管理员</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="css/AddAdmin.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>
                <img src="../imgs/shark.png" alt="">
                CoolShark商城管理系统
                <span v-if="" style="float: right;font-size: 15px;">
                    <a href="JavaScript:void(0)" style="text-decoration: none ;font-family: 楷体">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside>
                <!-- :unique-opened="true":设置子菜单只允许打开一个--->
                <el-menu
                        default-active="2-1"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-user-solid"></i>
                            <span>管理员管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="2-1" v-if="username==='root'">添加管理员</el-menu-item>
                            <el-menu-item index="2-2">管理员列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-s-claim"></i>
                            <span>类别管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="3-1">添加类别</el-menu-item>
                            <el-menu-item index="3-2">类别列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="4-1">添加商品</el-menu-item>
                            <el-menu-item index="4-2">商品列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">添加轮播图</el-menu-item>
                            <el-menu-item index="5-2">轮播图列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-menu-item index="6">
                        <i class = "el-icon-user-solid"></i>
                        <span>个人信息</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <el-card>
                    <h1>添加管理员</h1>
                    <el-form :model="admin" :rules="rules" ref="adminForm" label-width="80px">
                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="admin.username" placeholder="请输入以G开头的4-10位用户名.例如:G0001"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input v-model="admin.password" show-password placeholder="请输入密码"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名" prop="name">
                            <el-input v-model="admin.name" placeholder="请输入姓名"></el-input>
                        </el-form-item>
                        <el-form-item label="手机号" prop="phone">
                            <el-input v-model="admin.phone" placeholder="请输入手机号"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="admin.email" placeholder="请输入邮箱"></el-input>
                        </el-form-item>
                        <el-form-item label="角色" prop="role">
                            <el-select v-model="admin.role" placeholder="---请选择---">
                                <el-option label="管理员" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="addAdmin()">添加</el-button>
                            <el-button type="primary" @click="resetForm()">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                admin: {
                    name:'',
                    username: '',
                    password: '',
                    phone: '',
                    email: '',
                    role: ''
                },
                username: localStorage.getItem('username'),
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur'},
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        {
                            relax: /^(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
                            message: '请输入正确的手机号',
                            trigger: 'blur'
                        }
                    ],
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                        {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
                    ],
                    email: [
                        {required: true, message: '请输入邮箱', trigger: 'blur'},
                        {
                            pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
                            message: '请输入正确的邮箱地址',
                            trigger: 'blur'
                        }
                    ],
                    role: [
                        {required: true, message: '请选择角色', trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            addAdmin() {
                console.log(v.admin);
                axios.post('/admin/addAdmin', v.admin).then(function (response) {
                    let resonseBody = response.data;
                    if (resonseBody.state === 20000) {
                        v.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        // location.href = 'adminList.html';
                    } else {
                        alert(resonseBody.message);
                    }
                })
            },
            resetForm() {
                v.admin.username = '';
                v.admin.password = '';
                v.admin.phone = '';
                v.admin.email = '';
                v.admin.role = '';
                v.admin.name = '';
            },
            handleSelect(key, keyPath) {
                console.log(v.username);
                if (key === '1') {
                    location.href = 'index.html'; // 跳转到添加管理员页面
                } else if (key === '2-2') {
                    location.href = 'AdminList.html'; // 跳转到管理员列表页面
                } else if (key === '3-1') {
                    let name = prompt("请输入类别名称：", "");
                    if (name === null) {
                        return;
                    }
                    if (name.trim() === '') {
                        v.$message({
                            message: '类别名称不能为空！',
                            type: 'warning'
                        });
                    }
                    axios.get('/admin/insertCarousel?name=' + name)
                        .then(function (response) {
                            v.carousel = response.data.data;
                            location.href = 'CarouselList.html'
                        });
                } else if (key === '3-2') {
                    location.href = 'CarouselList.html'; // 跳转到类别列表页面
                } else if (key === '4-1') {
                    location.href = 'AddGoods.html'
                } else if (key === '4-2') {
                    location.href = 'GoodsList.html'
                } else if (key === '5-1') {
                    location.href = 'AddBanner.html'
                } else if (key === '5-2') {
                    location.href = 'BannerList.html'
                }else if (key === '6') {
                    location.href = 'PersonalInfo.html'
                }
            },
            handleOpen(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleClose(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
        }
    })
</script>
</html>